package com.glink.manage.config;

import com.glink.manage.interceptor.GrantCodeInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {


    @Value("${swagger.enabled:false}")
    private boolean swaggerEnable;

    @Autowired
    private DisablePageInterceptor disablePageInterceptor;

    @Autowired
    private GrantCodeInterceptor grantCodeInterceptor;
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
        if(swaggerEnable) {
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");

            registry.addResourceHandler("doc.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
        }
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if(!swaggerEnable) {
            registry.addInterceptor(disablePageInterceptor)
                    .addPathPatterns("/**"); // 这里可以指定需要应用拦截器的路径
        }

        registry.addInterceptor(grantCodeInterceptor).addPathPatterns("/**").excludePathPatterns("/error");
    }
}
